//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
/*
 *  linux/drivers/misc/wm9712.h
 *
 *  Copyright (C) 2001 Russell King, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 */
#ifndef _WM9712_H_
#define _WM9712_H_

#define WM9712_ADC_X				0x0001
#define WM9712_ADC_Y				0x0002
#define WM9712_ADC_P				0x0003

/* register address of WM9712 touch screen part */
#define  WM9712_DIGITISER_GCONF			0x004C
#define  WM9712_DIGITISER_GPTYPE			0x004E
#define  WM9712_DIGITISER_GSTICKY			0x0050
#define  WM9712_DIGITISER_GWAKEUP		0x0052
#define  WM9712_DIGITISER_GSTATUS			0x0054
#define  WM9712_DIGITISER_GSHARE			0x0056
//#define  WM9712_DIGITISER_AFUNC1			0x0058
#define  WM9712_DIGITISER_AFUNC1			0x005A
#define  WM9712_DIGITISER_AFUNC2			0x005C

#define WM9712_DIGITISER_DATA				0x007A
#define WM9712_DIGITISER_CONTROL1		0x0076
#define WM9712_DIGITISER_CONTROL2		0x0078

#define WM9712_GPIOBIT_IRQ					0x0004
#define WM9712_GPIOBIT_PENDOWN			0x2000

/* mask definition for Touch Controller 1 */
/* starts a single polled conversion (reset itself at completion) */
//#define WM9712_TOUCHCTRL1_POLL			(0x0001 << 15)
#define WM9712_TOUCHCTRL1_POLL			(0x0001 << 9)// andy

/* sets the address of the single conversion in polled mode, or extra conversion in coordinate or continous mode */
#define WM9712_TOUCHCTRL1_ADR_MASK		0x7000

//#define WM9712_TOUCHCTRL1_ADR_X			0x1000
//#define WM9712_TOUCHCTRL1_ADR_Y			0x2000
//#define WM9712_TOUCHCTRL1_PRESSURE		        0x3000
#define WM9712_TOUCHCTRL1_ADR_X			0x0002
#define WM9712_TOUCHCTRL1_ADR_Y			0x0004
#define WM9712_TOUCHCTRL1_PRESSURE	  	0x0006
#if 0 //20040819
#define WM9712_TOUCHCTRL1_BMON			0x4000
#define WM9712_TOUCHCTRL1_AUXADC		0x5000
#define WM9712_TOUCHCTRL1_PHONE			0x6000
#define WM9712_TOUCHCTRL1_PCBEEP		0x7000
#else
#define WM9712_TOUCHCTRL1_COMP1AUX1			0x4000
#define WM9712_TOUCHCTRL1_COMP1AUX2		0x5000
#define WM9712_TOUCHCTRL1_BMONAUX3			0x6000
#define WM9712_TOUCHCTRL1_WIPERAUX4		0x7000
#endif //20040819

/* selects a co-ordinate set of conversion */
#define WM9712_TOUCHCTRL1_COO			(0x0001 << 11)

/* starts continuous conversion operation */
#define WM9712_TOUCHCTRL1_CTC			(0x0001 << 10)

/* rate of continuous converation */
#define WM9712_TOUCHCTRL_CR_MASK		0x0300

#define WM9712_TOUCHCTRL1_CR_0		0x0000 // 93.75Hz  or 512 AC link frames
#define WM9712_TOUCHCTRL1_CR_1		0x0100 // 187.5Hz  or 256 AC link frame
#define WM9712_TOUCHCTRL1_CR_2		0x0200 // 375Hz    or 128 AC link frame
#define WM9712_TOUCHCTRL1_CR_3		0x0300 // 750Hz    or 64  AC link frame

/* set the delay between screen being driven and X or Y sample being taken */
#define WM9712_TOUCHCTRL1_DEL_MASK	0x00F0

#if 0 //20040819
#define WM9712_TOUCHCTRL1_DEL_0		0x0000 // delay one frame
#define WM9712_TOUCHCTRL1_DEL_1		0x0010 // 2
#define WM9712_TOUCHCTRL1_DEL_2		0x0020 // 4
#define WM9712_TOUCHCTRL1_DEL_3		0x0030 // 6
#define WM9712_TOUCHCTRL1_DEL_4		0x0040 // 8
#define WM9712_TOUCHCTRL1_DEL_5		0x0050 // 16
#define WM9712_TOUCHCTRL1_DEL_6		0x0060 // 32
#define WM9712_TOUCHCTRL1_DEL_7		0x0070 // 64
#else
#define WM9712_TOUCHCTRL1_DEL_0		0x0000 //
#define WM9712_TOUCHCTRL1_DEL_1		0x0010 //
#define WM9712_TOUCHCTRL1_DEL_2		0x0020 //
#define WM9712_TOUCHCTRL1_DEL_3		0x0030 //
#define WM9712_TOUCHCTRL1_DEL_4		0x0040 //
#define WM9712_TOUCHCTRL1_DEL_5		0x0050 //
#define WM9712_TOUCHCTRL1_DEL_6		0x0060 //
#define WM9712_TOUCHCTRL1_DEL_7		0x0070 //
#define WM9712_TOUCHCTRL1_DEL_8		0x0080 //
#define WM9712_TOUCHCTRL1_DEL_9		0x0090 //
#define WM9712_TOUCHCTRL1_DEL_a		0x00a0 //
#define WM9712_TOUCHCTRL1_DEL_b		0x00b0 //
#define WM9712_TOUCHCTRL1_DEL_c		0x00c0 //
#define WM9712_TOUCHCTRL1_DEL_d		0x00d0 //
#define WM9712_TOUCHCTRL1_DEL_e		0x00e0 //
#define WM9712_TOUCHCTRL1_DEL_f		0x00f0 //
#endif //20040819

/* slot enable bit; allows conversion results to be outpput onto chosen slot */
#define WM9712_TOUCHCTRL1_SLEN        (0x0001 << 3)

/*slot select bits, choose which slot to output data result in */
#if 0 //20040819
#define WM9712_TOUCHCTRL1_SLT_5		0x000 // Slot 5
#define WM9712_TOUCHCTRL1_SLT_6		0x001 // Slot 6
#define WM9712_TOUCHCTRL1_SLT_7		0x010 // Slot 7
#define WM9712_TOUCHCTRL1_SLT_8		0x011 // Slot 8
#define WM9712_TOUCHCTRL1_SLT_9		0x100 // Slot 9
#define WM9712_TOUCHCTRL1_SLT_10	0x101 // Slot 10
#define WM9712_TOUCHCTRL1_SLT_11	0x110 // Slot 11
#else
#define WM9712_TOUCHCTRL1_SLT_5		0x0000 // Slot 5
#define WM9712_TOUCHCTRL1_SLT_6		0x0001 // Slot 6
#define WM9712_TOUCHCTRL1_SLT_7		0x0002 // Slot 7
#define WM9712_TOUCHCTRL1_SLT_8		0x0003 // Slot 8
#define WM9712_TOUCHCTRL1_SLT_9		0x0004 // Slot 9
#define WM9712_TOUCHCTRL1_SLT_10		0x0005 // Slot 10
#define WM9712_TOUCHCTRL1_SLT_11		0x0006 // Slot 11
#endif //20040819

// Set Contiuous Mode up as follows
// SLT  = 000 - Slot5
// SLEN = 1
// DEL  = 0000
// CR   = 00
// CTC  = 1
// COO  = 1
// ADR  = 000
// POLL = 0

//#define WM9712_TOUCHCTRL1_CONT_MODE   0x0C08		// Continuous X, Y conversions
//#define WM9712_TOUCHCTRL1_CONT_MODE   0x3C08		// Continuous X, Y and Pressure conversions

//#define WM9712_TOUCHCTRL1_CONT_MODE   0x0C09		// Continuous X, Y conversions 93HZ
#define WM9712_TOUCHCTRL1_CONT_MODE   0x0d09		// Continuous X, Y conversions 187HZ
//#define WM9712_TOUCHCTRL1_CONT_MODE   0x0e09		// Continuous X, Y conversions 375HZ
//#define WM9712_TOUCHCTRL1_CONT_MODE   0x0f09		// Continuous X, Y conversions 750HZ
//#define WM9712_TOUCHCTRL1_CONT_MODE   0x3d09		// Continuous X, Y, pressure conversions 187HZ
//#define WM9712_TOUCHCTRL2_CONT_MODE		0xe810
#define WM9712_TOUCHCTRL2_CONT_MODE		0xe010	//measure regardless of pen status

#define WM9712_TOUCHCTRL2_POLL_TEST   0xC400
#define WM9712_TOUCHCTRL1_POLL_TEST_X   0x9008
#define WM9712_TOUCHCTRL1_POLL_TEST_Y   0xA008

/* Pen digitiser power down bit; tuns on or off al pen functions; 0: power on ; 1: power off */
#define WM9712_TOUCHCTRL2_PRP         (0x0001 << 15)

/* enables pr4 reset on pen down wake up */
#define WM9712_TOUCHCTRL2_PDW         (0x0001 << 14)

/* for rev C of WM9712 PRP and PDW are renamed to PRP[1:0] */

#define WM9712_TOUCHCTRL2_PRP_PDW_3     0xC000   // pen digitiser powered up
#define WM9712_TOUCHCTRL2_PRP_PDW_2     0x8000   // pen digitiser powered off, pen detect enabled, no wakeup on pen down
#define WM9712_TOUCHCTRL2_PRP_PDW_1     0x4000   // powered off, pen detect enabled, wakeup on pendown
#define WM9712_TOUCHCTRL2_PRP_PDW_0     0x0000   // powered down, pen detect disabled, no wakeup on pendown


/* Pen-Down Wake-up disable; setting this bit prevents PRP from being reset if pen down is detected, so stopping powering up of th digitiser */
#define WM9712_TOUCHCTRL2_RPR         (0x0001 << 13)

#if 1 //20040819
/*touch screen is 4-wire or 5-wire*/
#define WM9712_TOUCHCTRL2_45W         (0x0001 << 12)
#endif//20040819

#if 0 //20040819
/* causes conversions and screen drives to stop when pen is not down */
#define WM9712_TOUCHCTRL2_PDEN        (0x0001 << 12)
#else
#define WM9712_TOUCHCTRL2_PDEN        (0x0001 << 11)
#endif//20040819

#if 0 //20040819
/* inverts sense of pendet outputl default 0 causes pen=down to give a 1 output */
#define WM9712_TOUCHCTRL2_PINV        (0x0001 << 11)

/* busy flag enable, outputs converter busy signal on pin 47 */
/* goes low at end of conversion to indicate result available */
#define WM9712_TOUCHCTRL2_BSEN        (0x0001 << 10)

/* invert sense of busy flag output */
#define WM9712_TOUCHCTRL2_BINV        (0x0001 << 9)
#endif//20040819

#if 0 //20040819
/* wait bitl setting this bit causes conversion operations to halt when a result */
/* is to be written to the dat aregister 7Ah, if the last stored result has not */
/* been read. conversions commence again once the result has been read */
#define WM9712_TOUCHCTRL2_WAIT        (0x0001 << 8)
#else
#define WM9712_TOUCHCTRL2_WAIT        (0x0001 << 9)
#endif//20040819

#if 0 //20040819
/* pressure current reduce bit (0: 200uA; 1: 50 uA) */
#define WM9712_TOUCHCTRL2_PIL         (0x0001 << 7)
#else
/* pressure current reduce bit (0: 200uA; 1: 400 uA) */
#define WM9712_TOUCHCTRL2_PIL         (0x0001 << 8)
#endif//20040819

/* sets phone and pcbeep inputs to be high impedance (disconnects from mixer) */
//#define WM9712_TOUCHCTRL2_PHIZ        (0x0001 << 6)


// select what effect mask input:
// 00: masks pin ignored;
// 01: mask pin is static; hi halts conversion
// 10: mask pin is edge sensitive; rising or falling delays convrsions by del[0-3]
// 11: mask pin becomes a synchronise input; conversion happen after edge

#if 0 //20040819
#define WM9712_TOUCHCTRL2_MASK        0x0030

#define WM9712_TOUCHCTRL2_MSK_0       0x0000
#define WM9712_TOUCHCTRL2_MSK_1       0x0010
#define WM9712_TOUCHCTRL2_MSK_2       0x0020
#define WM9712_TOUCHCTRL2_MSK_3       0x0030

/*pen detect comparator threshold; 16 steps between 0 and Vmid */

#define WM9712_TOUCHCTRL2_PDD_0       0x0000
#define WM9712_TOUCHCTRL2_PDD_1       0x0001
#define WM9712_TOUCHCTRL2_PDD_2       0x0002
#define WM9712_TOUCHCTRL2_PDD_3       0x0003
#define WM9712_TOUCHCTRL2_PDD_4       0x0004
#define WM9712_TOUCHCTRL2_PDD_5       0x0005
#define WM9712_TOUCHCTRL2_PDD_6       0x0006
#define WM9712_TOUCHCTRL2_PDD_7       0x0007
#define WM9712_TOUCHCTRL2_PDD_8       0x0008
#define WM9712_TOUCHCTRL2_PDD_9       0x0009
#define WM9712_TOUCHCTRL2_PDD_10      0x000A
#define WM9712_TOUCHCTRL2_PDD_11      0x000B
#define WM9712_TOUCHCTRL2_PDD_12      0x000C
#define WM9712_TOUCHCTRL2_PDD_13      0x000D
#define WM9712_TOUCHCTRL2_PDD_14      0x000E
#define WM9712_TOUCHCTRL2_PDD_15      0x000F
#else
#define WM9712_TOUCHCTRL2_MASK        0x00c0

#define WM9712_TOUCHCTRL2_MSK_0       0x0000
#define WM9712_TOUCHCTRL2_MSK_1       0x0040
#define WM9712_TOUCHCTRL2_MSK_2       0x0080
#define WM9712_TOUCHCTRL2_MSK_3       0x00c0

/*pen detect comparator threshold; 64 steps between 0 and Vmid */

#define WM9712_TOUCHCTRL2_PDD_0       0x0000
#define WM9712_TOUCHCTRL2_PDD_1       0x0001
#define WM9712_TOUCHCTRL2_PDD_2       0x0002
#define WM9712_TOUCHCTRL2_PDD_3       0x0003
#define WM9712_TOUCHCTRL2_PDD_4       0x0004
#define WM9712_TOUCHCTRL2_PDD_5       0x0005
#define WM9712_TOUCHCTRL2_PDD_6       0x0006
#define WM9712_TOUCHCTRL2_PDD_7       0x0007
#define WM9712_TOUCHCTRL2_PDD_8       0x0008
#define WM9712_TOUCHCTRL2_PDD_9       0x0009
#define WM9712_TOUCHCTRL2_PDD_10      0x000A
#define WM9712_TOUCHCTRL2_PDD_11      0x000B
#define WM9712_TOUCHCTRL2_PDD_12      0x000C
#define WM9712_TOUCHCTRL2_PDD_13      0x000D
#define WM9712_TOUCHCTRL2_PDD_14      0x000E
#define WM9712_TOUCHCTRL2_PDD_15      0x000F
#define WM9712_TOUCHCTRL2_PDD_16       0x0010
#define WM9712_TOUCHCTRL2_PDD_17       0x0011
#define WM9712_TOUCHCTRL2_PDD_18       0x0012
#define WM9712_TOUCHCTRL2_PDD_19       0x0013
#define WM9712_TOUCHCTRL2_PDD_20       0x0014
#define WM9712_TOUCHCTRL2_PDD_21       0x0015
#define WM9712_TOUCHCTRL2_PDD_22       0x0016
#define WM9712_TOUCHCTRL2_PDD_23       0x0017
#define WM9712_TOUCHCTRL2_PDD_24       0x0018
#define WM9712_TOUCHCTRL2_PDD_25       0x0019
#define WM9712_TOUCHCTRL2_PDD_26      0x001A
#define WM9712_TOUCHCTRL2_PDD_27      0x001B
#define WM9712_TOUCHCTRL2_PDD_28      0x001C
#define WM9712_TOUCHCTRL2_PDD_29      0x001D
#define WM9712_TOUCHCTRL2_PDD_30      0x001E
#define WM9712_TOUCHCTRL2_PDD_31      0x001F
#define WM9712_TOUCHCTRL2_PDD_32       0x0020
#define WM9712_TOUCHCTRL2_PDD_33       0x0021
#define WM9712_TOUCHCTRL2_PDD_34       0x0022
#define WM9712_TOUCHCTRL2_PDD_35       0x0023
#define WM9712_TOUCHCTRL2_PDD_36       0x0024
#define WM9712_TOUCHCTRL2_PDD_37       0x0025
#define WM9712_TOUCHCTRL2_PDD_38       0x0026
#define WM9712_TOUCHCTRL2_PDD_39       0x0027
#define WM9712_TOUCHCTRL2_PDD_40       0x0028
#define WM9712_TOUCHCTRL2_PDD_41       0x0029
#define WM9712_TOUCHCTRL2_PDD_42      0x002A
#define WM9712_TOUCHCTRL2_PDD_43      0x002B
#define WM9712_TOUCHCTRL2_PDD_44      0x002C
#define WM9712_TOUCHCTRL2_PDD_45      0x002D
#define WM9712_TOUCHCTRL2_PDD_46      0x002E
#define WM9712_TOUCHCTRL2_PDD_47      0x002F
#define WM9712_TOUCHCTRL2_PDD_48       0x0030
#define WM9712_TOUCHCTRL2_PDD_49       0x0031
#define WM9712_TOUCHCTRL2_PDD_50       0x0032
#define WM9712_TOUCHCTRL2_PDD_51       0x0033
#define WM9712_TOUCHCTRL2_PDD_52       0x0034
#define WM9712_TOUCHCTRL2_PDD_53       0x0035
#define WM9712_TOUCHCTRL2_PDD_54       0x0036
#define WM9712_TOUCHCTRL2_PDD_55       0x0037
#define WM9712_TOUCHCTRL2_PDD_56       0x0038
#define WM9712_TOUCHCTRL2_PDD_57       0x0039
#define WM9712_TOUCHCTRL2_PDD_58      0x003A
#define WM9712_TOUCHCTRL2_PDD_59      0x003B
#define WM9712_TOUCHCTRL2_PDD_60      0x003C
#define WM9712_TOUCHCTRL2_PDD_61      0x003D
#define WM9712_TOUCHCTRL2_PDD_62      0x003E
#define WM9712_TOUCHCTRL2_PDD_63      0x003F
#endif //20040819

/* mask definistion for Touch Data register */
#define WM9712_DATA_PENDOWN          (0x0001 << 15) // used to check the status of bit 15 of register 7A
                                                    // indicates whether pen is down; 1: down
/* conversion address, indicates which channel the following result is for */
#define WM9712_DATA_ADC_MASK         0x7000

#define WM9712_DATA_ADR_X            0x1000
#define WM9712_DATA_ADR_Y            0x2000
#define WM9712_DATA_PRESSURE         0x3000
#if 0 //20040819
#define WM9712_DATA_BMON             0x4000
#define WM9712_DATA_AUXADC           0x5000
#define WM9712_DATA_PHONE            0x6000
#define WM9712_DATA_PCBEEP           0x7000
#else
#define WM9712_DATA_COMP1AUX1             0x4000
#define WM9712_DATA_COMP2AUX           0x5000
#define WM9712_DATA_BMONAUX3            0x6000
#define WM9712_DATA_WIPERAUX4           0x7000
#endif //20040819

#define WM9712_DATA_VAL              0x0FFF // adc output result

#define WM9712_NOSYNC	(0)
#define WM9712_SYNC	(1)


//unsigned int sampleADC(struct wm9712 *wm9712, unsigned int *sample, unsigned int axis);
//void wm9712_adc_enable(struct wm9712 *wm9712);
//void wm9712_adc_disable(struct wm9712 *wm9712);

/*
 * Which edges of the IRQ do you want to control today?
 */
#define WM9712_RISING	(1 << 0)
#define WM9712_FALLING	(1 << 1)

//int wm9712_hook_irq(struct wm9712 *wm9712, void (*fn)( void *), void *devid);
//void wm9712_enable_irq(struct wm9712 *wm9712);
//void wm9712_disable_irq(struct wm9712 *wm9712);

#ifdef __cplusplus
extern "C" {
#endif
int wm9712_configure();
void wm9712_configure_GPIO();
void WM9712_clear_sticky( void );
unsigned int sampleADC(unsigned int *sample, unsigned int axis);

/*
 * Switch to pressure mode, and read pressure.  We don't need to wait
 * here, since both plates are being driven.
 */
__inline unsigned int WM9712_ts_read_pressure()
{
	unsigned int val;
	return sampleADC(&val, WM9712_ADC_P);
	//return val;
}

__inline unsigned int WM9712_ts_read_xpos()
{
	unsigned int val;
	sampleADC(&val, WM9712_ADC_X);
//	sampleADC(&val, WM9712_ADC_Y);//inverted
	return val;
}

__inline unsigned int WM9712_ts_read_ypos()
{
	unsigned int val;
	sampleADC(&val, WM9712_ADC_Y);
//	sampleADC(&val, WM9712_ADC_X);//inverted
	return val;
}

#ifdef __cplusplus
}
#endif

#endif // _WM9712_H_
